package cn.ccit.action;

import java.util.List;

import cn.ccit.base.BaseConstants;
import cn.ccit.entites.Terminal;
import cn.ccit.iservers.ITerminalServers;
import cn.ccit.model.BaseModel;
import cn.ccit.servers.TerminalServers;
import cn.ccit.util.GsonUtil;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ModelDriven;

public class TerminalAction extends BaseAction implements ModelDriven<Terminal>{
	private Terminal terminal=new Terminal();
	private ITerminalServers iterm=new TerminalServers();
	 
	
	public String addTerminal(){
		BaseModel<Terminal> baseModel=new BaseModel<Terminal>();
		if(iterm.addTerminal(terminal)){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	
	public String removeTerminal() {
		BaseModel<Terminal> baseModel=new BaseModel<Terminal>();
		if(iterm.removeTerminal(terminal)){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	
	public String changeTerminalInfo() {
		BaseModel<Terminal> baseModel=new BaseModel<Terminal>();
		if(iterm.changeTerminalInfo(terminal)){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	public String searchTerminalByID() {
		BaseModel<Terminal> baseModel=new BaseModel<Terminal>();
		if(iterm.searchTerminalByID(terminal.getId())!=null){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			baseModel.setData(iterm.searchTerminalByID(terminal.getId()));
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	public String getSomeTerminals() {
		BaseModel<List<Terminal>> baseModel=new BaseModel<List<Terminal>>();
		if(iterm.getSomeTerminals(startId, limit)!=null&&
				iterm.getSomeTerminals(startId, limit).size()>0){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			baseModel.setData(iterm.getSomeTerminals(startId, limit));
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	
	public String searchTerminalByName() {
		BaseModel<List<Terminal>> baseModel=new BaseModel<List<Terminal>>();
		if(iterm.searchTerminalByName(terminal.getName())!=null&&
				iterm.searchTerminalByName(terminal.getName()).size()>0){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			baseModel.setData(iterm.searchTerminalByName(terminal.getName()));
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	public String searchTerminalByHostName() {
		BaseModel<List<Terminal>> baseModel=new BaseModel<List<Terminal>>();
		if(iterm.searchTerminalByHostName(terminal.getHostName())!=null&&
				iterm.searchTerminalByHostName(terminal.getHostName()).size()>0){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			baseModel.setData(iterm.searchTerminalByHostName(terminal.getHostName()));
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	
	public String searchTerminalByPhone() {
		BaseModel<Terminal> baseModel=new BaseModel<Terminal>();
		if(iterm.searchTerminalByPhone(terminal.getPhone())!=null){
			baseModel.setStatus(BaseConstants.SUCESSFUL_STATE_CODE);
			baseModel.setMessage(BaseConstants.SUCESSFUL_STATE_MESSAGE);
			baseModel.setData(iterm.searchTerminalByPhone(terminal.getPhone()));
		}else{
			baseModel.setStatus(BaseConstants.FAIL_STATE_CODE);
			baseModel.setMessage(BaseConstants.FAIL_STATE_MESSAGE);
		}
		GsonUtil.sendJson(baseModel);
		return Action.SUCCESS;
	}
	
	@Override
	public Terminal getModel() {
		return terminal;
	}
	
}
